package com.miao.im.support.tester;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.IdUtil;
import com.miao.im.common.Constant;
import com.miao.im.common.enums.Command;
import com.miao.im.common.enums.MessageType;
import com.miao.im.model.AuthRequestModel;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/5/14 15:50
 * @Description:
 */
public class AuthRequestTester {
    public static void main(String[] args) {
        AuthRequestModel.AuthRequest authRequest = AuthRequestModel.AuthRequest.newBuilder()
                .setUsername("3159c301af3f473db48824907fc62b02")
                .setPassword("")
                .setDeviceNumber("c80b803e-ea2c-42be-b708-905b34df0e4b")
                .build();
        byte[] dataBytes = authRequest.toByteArray();
        ByteBuffer buffer = ByteBuffer.allocate(Constant.HEADER_LENGTH + dataBytes.length);
        buffer.putInt(Constant.HEADER_LENGTH);
        buffer.putInt(1);
        buffer.putInt(MessageType.REQ.getCode());
        buffer.putLong(IdUtil.getSnowflake(1, 1).nextId());
        buffer.putInt(Command.AUTH.getCode());
        buffer.putInt(dataBytes.length);
        buffer.put(dataBytes);
        System.out.println(Arrays.toString(buffer.array()));
        System.out.println(HexUtil.encodeHexStr(buffer.array()));
    }

}
